package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.AirLane;
import com.example.entity.Work;

import com.example.entity.MicroPage;
import com.example.mapper.WorkMapper;
import com.example.service.interfaces.WorkInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @program: microservice
 * @description:
 * @author: Aprilies
 * @create: 2024-09-10 16:51
 **/

@Service
public class WorkService extends ServiceImpl<WorkMapper, Work> implements WorkInterface {

    @Autowired
    private WorkMapper workMapper;

    public List<Work> findAll(){return workMapper.selectList(null);}
    public Optional<Work> findByID(Long id){return Optional.ofNullable(workMapper.selectById(id));}

    public  Work isave(Work user){
        workMapper.insert(user);
        return user;
    }

    public Optional<Work> update(Work work){
        int i = workMapper.updateById(work);
        if (i == 1){
            return Optional.of(work);
        }
        return Optional.empty();
    }

    // 返回所有字段数据
    public Optional<Work> update2(Integer id, Work work) {
        Work originwork = workMapper.selectById(id);
        if (work.getStartStopState() != null) {
            originwork.setStartStopState(work.getStartStopState());
        }
        if (work.getCorrespondingTimePeriod() != null) {
            originwork.setCorrespondingTimePeriod(work.getCorrespondingTimePeriod());
        }
        if (work.getCapacityThreshold() != null) {
            originwork.setCapacityThreshold(work.getCapacityThreshold());
        }
        if (work.getSafeIntervalThreshold() != null) {
            originwork.setSafeIntervalThreshold(work.getSafeIntervalThreshold());
        }
        int i = workMapper.updateById(originwork);
        if(i == 1){
            return Optional.of(originwork);
        }
        return Optional.empty();
    }

    public boolean delete(Long id) {
        if (workMapper.selectById(id) != null) {
            workMapper.deleteById(id);
            return true;
        }
        return false;
    }

    public MicroPage<Work> page(int current, int size, Work condition) {
        QueryWrapper<Work> wrapper = new QueryWrapper<>();
        if(condition != null && condition.getAircraftType() != null){
            wrapper.like("application_id", condition.getAircraftType());
        }
        Page<Work> page = new Page<>(current, size);
        Page<Work> WorkPage = workMapper.selectPage(page, wrapper);
        int total = (int)WorkPage.getTotal();
        int pageSize = (int)page.getSize();
        MicroPage<Work> WorkMicroPage = new MicroPage<>();
        WorkMicroPage.setTotal(total);
        WorkMicroPage.setPageData(WorkPage.getRecords());
        WorkMicroPage.setPageNum(current);
        WorkMicroPage.setPageSize(size);
        Integer pages = (int) Math.ceil((double) total / pageSize);
        WorkMicroPage.setPages(pages);
        return WorkMicroPage;
    }
}
